		$(document).ready(function() {
			var userId = $.cookie('userId');
			$.ajax({
                type: "post",
                async: false,
                url: "/eshop-project/addr/getCount?UserID=" + userId,
                success: function (data) {
                	$("#count").text(data);
                	var count = data;
                	$.ajax({
                        type: "post",
                        async: false,
                        url: "/eshop-project/addr/loadAll?UserID=" + userId,
                        success: function (msg) {
        					var html = "";
        					for (var i = 0; i < count; i++) {
        						html += '<li class="list-group-item d-flex justify-content-between">' +
        								'<div>' +
        								'<h6 class="my-0">' + msg[i].consignee + '<small class="ml-4">' + msg[i].phone + '</small></h6>' +
		              					'<small>' + msg[i].detailAdd + '</small>' +
	                					'</div>' + '<span style="cursor: pointer;"><a class="fa fa-edit" onclick="$.getAddrById('+ msg[i].addId +');"></a></span>' +
	              						'</li>';
        					}
        					$("#addrList").html(html);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest.status);
                            alert(XMLHttpRequest.readyState);
                            alert(textStatus);
                        },
                    });
                	
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                },
            });
			
			$("#save").click(function(){
				var consignee = $('input[id="consignee"]').val();
				var detailAdd = $('input[id="area"]').val() + $('input[id="adress"]').val();
				var phone = $('input[id="phone"]').val();
            	$.ajax({
                    type: "post",
                    async: false,
                    url: "/eshop-project/addr/saveAddr?UserID=" + userId + "&consignee=" + consignee + "&detailAdd=" + detailAdd + "&phone=" + phone,
                    success: function (data) {
                    	console.log(data);
    					alert("添加地址信息成功！");
    					window.location.reload();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                });
			});
			
			$.getAddrById = function(addId) {
				$('#part-1').show();
				$('#part-2').hide();
				$('#form-update input[name="addId"]').val(addId);
				$.ajax({
                    type: "post",
                    async: false,
                    url: "/eshop-project/addr/getAddrById?AddId=" + addId,
                    success: function (data) {
                    	$('#form-update input[name="consignee"]').val(data.consignee);
                    	$('#form-update input[name="phone"]').val(data.phone);
                    	$('#form-update input[name="detailAdd"]').val(data.detailAdd);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                });

			}

			$("#btn-update").click(function(){
				var addId = $('#form-update input[name="addId"]').val();
				var consignee = $('#form-update input[name="consignee"]').val();
				var detailAdd = $('#form-update input[name="detailAdd"]').val();
				var phone = $('#form-update input[name="phone"]').val();
				var UserID = 3;
            	$.ajax({
                    type: "post",
                    async: false,
                    url: "/eshop-project/addr/updateAddr?addId=" + addId + "&consignee=" + 
                    		consignee + "&detailAdd=" + detailAdd + "&phone=" + phone + "&UserID=	" + userId,
                    success: function (data) {
                    	console.log(data);
    					alert("修改地址信息成功！");
    					window.location.reload();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                });
			});
			
			$("#btn-delete").click(function(){
				var addId = $('#form-update input[name="addId"]').val();
				$.ajax({
                    type: "post",
                    async: false,
                    url: "/eshop-project/addr/delAddr?AddrId=" + addId,
                    success: function (data) {
                    	console.log(data);
    					alert("删除地址信息成功！");
    					window.location.reload();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },
                });
			});
			
		});